package controller;

import entity.User;
import entity.UserDetail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import serviceimpl.UserDetailServiceImpl;
import serviceimpl.UserServiceImpl;
import util.MD5Util;

import java.text.SimpleDateFormat;
import java.util.Date;

@RestController
@RequestMapping("/user")
public class LoginAndRegisterController {

    @Autowired
    UserServiceImpl userService;

    @Autowired
    UserDetailServiceImpl userDetailService;



    @RequestMapping(value = "/register",produces = "text/html;charset=utf-8")
    @Transactional
    public String register(@RequestParam String username,@RequestParam String password,@RequestParam String name,@RequestParam String phone,@RequestParam String addr){
        System.out.println("进入register");
        User user=new User(username, MD5Util.MD5PassWord(password),new SimpleDateFormat("yyyy-MM-dd HH:ss:mm").format(new Date()));
        int line=userService.insert(user);
        if(line>0){
           int line2= userDetailService.insert(new UserDetail(user.getId(),name,phone,addr));
           if(line2>0){
               return "ok";
           }
        }

        return "no";
    }


    @RequestMapping(value = "/login",produces = "text/html;charset=utf-8")
    public String login(@RequestParam String username,@RequestParam String password){
        return userService.selectByUserName(username,password);
    }
}
